﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl x:Class="System.Windows.Controls.Samples.CustomEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input">
    <StackPanel>

        <ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" />
        <StackPanel>
            <TextBlock Style="{StaticResource Information}">
                The AutoCompleteBox control is able to work with unbound data as 
                well. By subscribing to the Populating event of an 
                AutoCompleteBox control, as a developer you can cancel the 
                population, change ItemsSource data, or otherwise react to the 
                input before the control provides suggestions.
            </TextBlock>
        </StackPanel>

        <ContentControl Content="Text completion off" Style="{StaticResource Header}" />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="230" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Padding="5">Immediate population:</TextBlock>

            <input:AutoCompleteBox 
                x:Name="NowAutoComplete" 
                FilterMode="None"
                Width="300" 
                IsTextCompletionEnabled="False"
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value1"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value1" Grid.Column="2" />

            <TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>

            <input:AutoCompleteBox 
                x:Name="LaterAutoComplete" 
                FilterMode="None"
                IsTextCompletionEnabled="False"
                Width="300" 
                Grid.Row="1" 
                Grid.Column="1" 
                HorizontalAlignment="Left" 
                Tag="Value2"
                SelectionChanged="OnSelectionChanged"
            />
            <ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" />

        </Grid>

        <ContentControl Content="Text completion on" Style="{StaticResource Header}" />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="230" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Padding="5">Immediate population:</TextBlock>

            <input:AutoCompleteBox 
                x:Name="NowAutoComplete2" 
                FilterMode="None"
                IsTextCompletionEnabled="True"
                Width="300" 
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value3"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value3" Grid.Column="2"/>

            <TextBlock Padding="5" Grid.Row="1">Delayed population:</TextBlock>

            <input:AutoCompleteBox
                x:Name="LaterAutoComplete2"
                FilterMode="None"
                IsTextCompletionEnabled="True"
                Width="300"
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                Tag="Value4"
                SelectionChanged="OnSelectionChanged"
                />
            <ContentPresenter x:Name="Value4"  Grid.Row="1" Grid.Column="2"/>
            
        </Grid>

        <src:SourceViewer xmlns:src="clr-namespace:System.Windows.Controls.Samples;assembly=System.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="CustomEvents.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl x:Class="System.Windows.Controls.Samples.CustomEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input"&gt;
    &lt;StackPanel&gt;

        &lt;ContentControl Content="Populating and Populated events" Style="{StaticResource Header}" /&gt;
        &lt;StackPanel&gt;
            &lt;TextBlock Style="{StaticResource Information}"&gt;
                The AutoCompleteBox control is able to work with unbound data as 
                well. By subscribing to the Populating event of an 
                AutoCompleteBox control, as a developer you can cancel the 
                population, change ItemsSource data, or otherwise react to the 
                input before the control provides suggestions.
            &lt;/TextBlock&gt;
        &lt;/StackPanel&gt;

        &lt;ContentControl Content="Text completion off" Style="{StaticResource Header}" /&gt;
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="230" /&gt;
                &lt;ColumnDefinition /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition /&gt;
                &lt;RowDefinition /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;TextBlock Padding="5"&gt;Immediate population:&lt;/TextBlock&gt;

            &lt;input:AutoCompleteBox 
                x:Name="NowAutoComplete" 
                FilterMode="None"
                Width="300" 
                IsTextCompletionEnabled="False"
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value1"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value1" Grid.Column="2" /&gt;

            &lt;TextBlock Padding="5" Grid.Row="1"&gt;Delayed population:&lt;/TextBlock&gt;

            &lt;input:AutoCompleteBox 
                x:Name="LaterAutoComplete" 
                FilterMode="None"
                IsTextCompletionEnabled="False"
                Width="300" 
                Grid.Row="1" 
                Grid.Column="1" 
                HorizontalAlignment="Left" 
                Tag="Value2"
                SelectionChanged="OnSelectionChanged"
            /&gt;
            &lt;ContentPresenter x:Name="Value2" Grid.Row="1" Grid.Column="2" /&gt;

        &lt;/Grid&gt;

        &lt;ContentControl Content="Text completion on" Style="{StaticResource Header}" /&gt;
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="230" /&gt;
                &lt;ColumnDefinition /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition /&gt;
                &lt;RowDefinition /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;TextBlock Padding="5"&gt;Immediate population:&lt;/TextBlock&gt;

            &lt;input:AutoCompleteBox 
                x:Name="NowAutoComplete2" 
                FilterMode="None"
                IsTextCompletionEnabled="True"
                Width="300" 
                Grid.Column="1" 
                HorizontalAlignment="Left"
                Margin="0, 0, 0, 12"
                Tag="Value3"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value3" Grid.Column="2"/&gt;

            &lt;TextBlock Padding="5" Grid.Row="1"&gt;Delayed population:&lt;/TextBlock&gt;

            &lt;input:AutoCompleteBox
                x:Name="LaterAutoComplete2"
                FilterMode="None"
                IsTextCompletionEnabled="True"
                Width="300"
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                Tag="Value4"
                SelectionChanged="OnSelectionChanged"
                /&gt;
            &lt;ContentPresenter x:Name="Value4"  Grid.Row="1" Grid.Column="2"/&gt;
            
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="CustomEvents.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

// Global suppressions for this sample
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "System.Windows.Controls.Samples.CustomEvents.#Value1")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "System.Windows.Controls.Samples.CustomEvents.#Value2")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "System.Windows.Controls.Samples.CustomEvents.#Value3")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "System.Windows.Controls.Samples.CustomEvents.#Value4")]

namespace System.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// The PopulationEvents class shows how a developer might hook into the 
    /// population events to provide custom data.
    /// &lt;/summary&gt;
    [Sample("(2)Custom Events", DifficultyLevel.Basic)]
    [Category("AutoCompleteBox")]
    public partial class CustomEvents : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the type.
        /// &lt;/summary&gt;
        public CustomEvents()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        /// &lt;summary&gt;
        /// Handle the Loaded event of the page.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event arguments.&lt;/param&gt;
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            NowAutoComplete.Populating += OnPopulatingSynchronous;
            NowAutoComplete2.Populating += OnPopulatingSynchronous;
            LaterAutoComplete.Populating += OnPopulatingAsynchronous;
            LaterAutoComplete2.Populating += OnPopulatingAsynchronous;
        }

        /// &lt;summary&gt;
        /// The Populating event handler.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnPopulatingSynchronous(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox source = (AutoCompleteBox)sender;

            source.ItemsSource = new string[]
            {
                e.Parameter + "1",
                e.Parameter + "2",
                e.Parameter + "3",
            };
        }

        /// &lt;summary&gt;
        /// The populating handler.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source object.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        private void OnPopulatingAsynchronous(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox source = (AutoCompleteBox)sender;

            // Cancel the populating value: this will allow us to call 
            // PopulateComplete as necessary.
            e.Cancel = true;
            
            // Use the dispatcher to simulate an asynchronous callback when 
            // data becomes available
            Dispatcher.BeginInvoke(
                delegate
                {
                    source.ItemsSource = new string[]
                    {
                        e.Parameter + "1",
                        e.Parameter + "2",
                        e.Parameter + "3",
                    };

                    // Population is complete
                    source.PopulateComplete();
                });
        }

        /// &lt;summary&gt;
        /// Called when an AutoCompleteBox's selected value changes. Uses the 
        /// Tag property to identify the content presenter to be updated.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source AutoCompleteBox control.&lt;/param&gt;
        /// &lt;param name="e"&gt;The event data.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler is wired up in XAML.")]
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AutoCompleteBox acb = (AutoCompleteBox)sender;

            // In these sample scenarios, the Tag is the name of the content 
            // presenter to use to display the value.
            string contentPresenterName = (string)acb.Tag;
            ContentPresenter cp = FindName(contentPresenterName) as ContentPresenter;
            if (cp != null)
            {
                cp.Content = acb.SelectedItem;
            }
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="CustomEvents.xaml.vb">
    <src:SourceFile.Source>
      <sys:String>' (c) Copyright Microsoft Corporation.
' This source is subject to the Microsoft Public License (Ms-PL).
' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
' All other rights reserved.

Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics.CodeAnalysis
Imports System.Windows
Imports System.Windows.Controls
Imports System.ComponentModel

' Global suppressions for this sample
&lt;Assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope := "member", Target := "System.Windows.Controls.Samples.CustomEvents.#Value1")&gt;
&lt;Assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope := "member", Target := "System.Windows.Controls.Samples.CustomEvents.#Value2")&gt;
&lt;Assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope := "member", Target := "System.Windows.Controls.Samples.CustomEvents.#Value3")&gt;
&lt;Assembly: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope := "member", Target := "System.Windows.Controls.Samples.CustomEvents.#Value4")&gt;

''' &lt;summary&gt;
''' The PopulationEvents class shows how a developer might hook into the 
''' population events to provide custom data.
''' &lt;/summary&gt;
&lt;Sample("(2)Custom Events", DifficultyLevel.Basic), Category("AutoCompleteBox")&gt; _
Partial Public Class CustomEvents
    Inherits UserControl
    ''' &lt;summary&gt;
    ''' Initializes a new instance of the type.
    ''' &lt;/summary&gt;
    Public Sub New()
        InitializeComponent()
    End Sub

    ''' &lt;summary&gt;
    ''' The Populating event handler.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The source object.&lt;/param&gt;
    ''' &lt;param name="e"&gt;The event data.&lt;/param&gt;
    Private Sub OnPopulatingSynchronous(ByVal sender As Object, ByVal e As PopulatingEventArgs) _
        Handles NowAutoComplete.Populating, NowAutoComplete2.Populating

        Dim source As AutoCompleteBox = CType(sender, AutoCompleteBox)

        source.ItemsSource = New String() _
        { _
            e.Parameter &amp; "1", _
            e.Parameter &amp; "2", _
            e.Parameter &amp; "3" _
        }
    End Sub

    ''' &lt;summary&gt;
    ''' The populating handler.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The source object.&lt;/param&gt;
    ''' &lt;param name="e"&gt;The event data.&lt;/param&gt;
    Private Sub OnPopulatingAsynchronous(ByVal sender As Object, ByVal e As PopulatingEventArgs) _
        Handles LaterAutoComplete.Populating, LaterAutoComplete2.Populating

        ' Cancel the populating value: this will allow us to call 
        ' PopulateComplete as necessary.
        e.Cancel = True

        ' Use the dispatcher to simulate an asynchronous callback when 
        ' data becomes available
        Dispatcher.BeginInvoke(New EventHandler(AddressOf PopulateHandler), sender, e)
    End Sub

    ''' &lt;summary&gt;
    ''' The asynchronous populating method.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The source object.&lt;/param&gt;
    ''' &lt;param name="e"&gt;The event data.&lt;/param&gt;
    Private Sub PopulateHandler(ByVal sender As Object, ByVal e As EventArgs)
        Dim source As AutoCompleteBox = CType(sender, AutoCompleteBox)
        Dim args As PopulatingEventArgs = CType(e, PopulatingEventArgs)

        source.ItemsSource = New String() _
        { _
            args.Parameter &amp; "1", _
            args.Parameter &amp; "2", _
            args.Parameter &amp; "3" _
        }

        ' Population is complete
        source.PopulateComplete()
    End Sub

    ''' &lt;summary&gt;
    ''' Called when an AutoCompleteBox's selected value changes. Uses the 
    ''' Tag property to identify the content presenter to be updated.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The source AutoCompleteBox control.&lt;/param&gt;
    ''' &lt;param name="e"&gt;The event data.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler is wired up in XAML.")&gt; _
    Private Sub OnSelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim acb As AutoCompleteBox = CType(sender, AutoCompleteBox)

        ' In these sample scenarios, the Tag is the name of the content 
        ' presenter to use to display the value.
        Dim contentPresenterName As String = CStr(acb.Tag)
        Dim cp As ContentPresenter = TryCast(FindName(contentPresenterName), ContentPresenter)
        If cp IsNot Nothing Then
            cp.Content = acb.SelectedItem
        End If
    End Sub
End Class
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>
